package com.das.mechanic_base.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class X3BaseLazyFragment<P extends X3BasePresenter> extends RxFragment implements X3IBaseView {

    protected P mPresenter;

    private Unbinder unbinder;

    private View mRootView;

    public BaseViewProxy viewProxy;

    private BroadcastReceiver mItemViewListClickReceiver;

    private boolean isLoaded = false;

    private boolean isHidden = true;

    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    @Override
    public Context getContext() {
        return getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (null != mRootView) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (null != parent) {
                parent.removeView(mRootView);
            }
        } else {
            mRootView = inflater.inflate(this.getLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, mRootView);
            mPresenter = createPresenter();
            if (mPresenter != null) {
                mPresenter.attachView(this);
            }
            initView(mRootView);
        }
        return mRootView;
    }

    /**
     * 是否设置透明背景
     * @return
     */
    @Override
    public boolean isTransparent() {
        return false;
    }

    protected abstract P createPresenter();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewProxy = new BaseViewProxy(getActivity());

        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.CART_BROADCAST");//建议把它写一个公共的变量，这里方便阅读就不写了。
        mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                System.out.println("OK");
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();

        }
        if (viewProxy != null) {
            viewProxy.destroy();
        }

        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mItemViewListClickReceiver);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (getActivity() != null) {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        if (getActivity() != null) {
            getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
        }
    }

    @Override
    public void showSuccess(String successMsg) {
        X3ToastUtils.showMessage(successMsg);
    }

    @Override
    public void showFaild(String errorMsg) {
         X3ToastUtils.showMessage(errorMsg);
    }

    @Override
    public void showNoNet() {
        X3ToastUtils.showMessage(getString(R.string.x3_network_error));
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();

    }

    @Override
    public void showMineDownNotice(String showType) {

    }

    public void showLoading(String text) {
        viewProxy.showLoading(text);
    }

    public void hideLoading() {
        viewProxy.hideLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        judgeLazyInit();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isHidden = hidden;
        judgeLazyInit();
    }

    private void judgeLazyInit() {
        if (!isLoaded && !isHidden) {
            lazyInit();
            isLoaded = true;
        }
        if (isLoaded && !isHidden) {
            everyInit();
        }
    }

    protected abstract void everyInit();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isLoaded = false;
    }

    protected abstract void lazyInit();

    @Override
    public void onPause() {
        super.onPause();
    }
}

